<?php
/**
 * 文件路径： \application\index\job\Hello.php
 * 这是一个消费者类，用于处理 helloJobQueue 队列中的任务
 */
namespace app\index\job;

use think\Db;
use think\queue\Job;

class JobActivity
{

    /**
     * fire方法是消息队列默认调用的方法
     * @param Job         $job  当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        file_put_contents(RUNTIME_PATH . 'fire.txt', date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);

        // 如有必要,可以根据业务需求和数据库中的最新数据,判断该任务是否仍有必要执行.
        $isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data);
        if (!$isJobStillNeedToBeDone) {
            $job->delete();
            return;
        }
        $isJobDone = $this->doActivity($data);

        if ($isJobDone) {
            //如果任务执行成功， 记得删除任务
            $job->delete();
        } else {
            if ($job->attempts() > 3) {
                //通过这个方法可以检查这个任务已经重试了几次了
                print("<warn>Job has been retried more than 3 times!" . "</warn>\n");
                $job->delete();
                // 也可以重新发布这个任务
                //print("<info>Hello Job will be availabe again after 2s."."</info>\n");
                //$job->release(2); //$delay为延迟时间，表示该任务延迟2秒后再执行
            }
        }
    }

    /**
     * 有些消息在到达消费者时,可能已经不再需要执行了
     * @param array|mixed $data 发布任务时自定义的数据
     * @return boolean                 任务执行的结果
     */
    private function checkDatabaseToSeeIfJobNeedToBeDone($data)
    {
        return true;
    }

    /**
     * 根据消息中的数据进行实际的业务处理
     * @param array|mixed $data 发布任务时自定义的数据
     * @return boolean                 任务执行的结果
     */
    private function doActivity($data)
    {
        file_put_contents(RUNTIME_PATH . 'doActivity.txt', date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
        echo '任务执行成功';
        /*$obj_id = $data['obj_id'];
        $type = $data['type'];
        $time = time();*/

        /*Db::startTrans();
        try {
            if ($type == 'group') {
                // 团购
                $group_info = Db::name('group')->where(['id' => $obj_id])->find();
                if(empty($group_info)){
                    Db::rollback();
                    print("团购活动不存在或已删除；obj_id：" . $obj_id);
                    return false;
                }

                if ($data['node'] == 'start') {
                    // 开始
                    $group_status = 1;
                    $goods_type = 1;
                } elseif ($data['node'] == 'end') {
                    // 结束
                    $group_status = 2;
                    $goods_type = 0;

                    Db::name('activity_log')->where(['goods_id' => $group_info['goods_id'], 'type' => 1])->delete();
                }
                $group_data = [
                    'status' => $group_status,
                    'update_time' => $time
                ];
                Db::name('group')->where(['id' => $obj_id])->update($group_data);

                Db::name('goods')->where(['id' => $group_info['goods_id']])->setField('activity_type', $goods_type);
            } elseif ($type == 'seckill') {
                // 秒杀
                $seckill_info = Db::name('seckill')->where(['id' => $obj_id])->find();
                if (empty($seckill_info)) {
                    Db::rollback();
                    print("秒杀活动不存在或已删除；obj_id：" . $obj_id);
                    return false;
                }

                if ($data['node'] == 'start') {
                    // 开始
                    $seckill_status = 1;
                    $goods_type = 2;
                } elseif ($data['node'] == 'end') {
                    // 结束
                    $seckill_status = 2;
                    $goods_type = 0;

                    Db::name('activity_log')->where(['goods_id' => $seckill_info['goods_id'], 'type' => 2])->delete();
                }
                $group_data = [
                    'status' => $seckill_status,
                    'update_time' => $time
                ];
                Db::name('seckill')->where(['id' => $obj_id])->update($group_data);

                Db::name('seckill')->where(['id' => $seckill_info['goods_id']])->setField('activity_type', $goods_type);
            }

            Db::commit();
            print("任务执行成功");
        }catch (\Exception $e){
            Db::rollback();
            print("<info>ERROR：". $e->getMessage() ." >>>>> 任务执行失败 ====>  job Data is: ". var_export($data, true) ."</info> \n");
            return false;
        }*/

        return true;
    }
}
